﻿package com.dam.monngonmoingay;

import com.dam.monngonmoingay.R;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.startapp.android.publish.StartAppAd;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MonAnActivity extends Activity {

	MonNgonDB monNgonDB;
	ImageView monAnImageView, facebookImageView, googleImageView,
			youtubeImageView;
	ImageButton backImageButton, yeuThichImageButton, nguyenLieuImageButton,
			cheBienImageButton, machNhoImageButton;
	TextView tenMonAnTextView, nguyenLieuTextView, cheBienTextView,
			meoVatTextView;

	StartAppAd startAppAd = new StartAppAd(this);

	Facebook fb = new Facebook(DeveloperKey.DEVELOPER_FACEBOOK_ID);

	String youtubeId;
	
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	String title,material,content,tips,image;
	int like = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		monNgonDB = new MonNgonDB(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.monan_layout);
		ImageLoader imageLoader = new ImageLoader(getApplicationContext());
		Bundle bundle = this.getIntent().getExtras();
		title = bundle.getString("title");
		image  = bundle.getString("image");
		String video = bundle.getString("video");
	    material = bundle.getString("material");
		content = bundle.getString("content");
		tips = bundle.getString("tips");
		like = bundle.getInt("like");
		youtubeId = video.replaceAll(
				"http://www.youtube.com/embed/", "");

		monAnImageView = (ImageView) findViewById(R.id.monAnImageView);
		imageLoader.DisplayImage(image, monAnImageView);

		youtubeImageView = (ImageView) findViewById(R.id.youtubeImageView);
		googleImageView = (ImageView) findViewById(R.id.googleImageView);
		facebookImageView = (ImageView) findViewById(R.id.facebookImageView);

		backImageButton = (ImageButton) findViewById(R.id.backImageButton);
		yeuThichImageButton = (ImageButton) findViewById(R.id.yeuThichImageButton);
		nguyenLieuImageButton = (ImageButton) findViewById(R.id.nguyenLieuImageButton);
		cheBienImageButton = (ImageButton) findViewById(R.id.cheBienImageButton);
		machNhoImageButton = (ImageButton) findViewById(R.id.meoVatImageButton);

		tenMonAnTextView = (TextView) findViewById(R.id.tenMonAnTextView);
		nguyenLieuTextView = (TextView) findViewById(R.id.nguyenLieuTextView);
		cheBienTextView = (TextView) findViewById(R.id.cheBienTextView);
		meoVatTextView = (TextView) findViewById(R.id.meoVatTextView);
		tenMonAnTextView.setText(Html.fromHtml(title));
		nguyenLieuTextView.setText(Html.fromHtml(material));
		cheBienTextView.setText(Html.fromHtml(content));
		meoVatTextView.setText(Html.fromHtml(tips));

		nguyenLieuTextView.setVisibility(View.GONE);
		cheBienTextView.setVisibility(View.GONE);
		meoVatTextView.setVisibility(View.GONE);

		Typeface type = Typeface.createFromAsset(getAssets(),
				"SEGOEPR.TTF");
		nguyenLieuTextView.setTypeface(type, Typeface.BOLD);
		cheBienTextView.setTypeface(type, Typeface.BOLD);
		meoVatTextView.setTypeface(type, Typeface.BOLD);
		tenMonAnTextView.setTypeface(type, Typeface.BOLD);

		if (like == 1) {
			yeuThichImageButton.setBackgroundResource(R.drawable.yellowstar);
		}

		facebookImageView.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (fb.isSessionValid()) {
					shareOnFacebook();
				} else {
					fb.authorize(MonAnActivity.this, new DialogListener() {

						@Override
						public void onComplete(Bundle values) {
							// TODO Auto-generated method stub
							shareOnFacebook();
						}

						@Override
						public void onFacebookError(FacebookError e) {
							// TODO Auto-generated method stub
							Toast.makeText(getApplicationContext(),
									R.string.shareError, Toast.LENGTH_SHORT).show();

						}

						@Override
						public void onError(DialogError e) {
							// TODO Auto-generated method stub
							Toast.makeText(getApplicationContext(), R.string.shareError,
									Toast.LENGTH_SHORT).show();

						}

						@Override
						public void onCancel() {
							// TODO Auto-generated method stub
							Toast.makeText(getApplicationContext(), R.string.shareCancel,
									Toast.LENGTH_SHORT).show();
						}

					});
				}
			}
		});

		youtubeImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						VideoActivity.class);
				intent.putExtra("VideoId", youtubeId);
				startActivity(intent);
			}
		});

		backImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(getApplicationContext(),
						ListMonAnActivity.class);
				Bundle bundle = new Bundle();
				MonNgonApplication app = (MonNgonApplication) getApplicationContext();
				bundle.putInt("id", app.getLastCatalogId());
				myIntent.putExtra("id", bundle);
				startActivity(myIntent);
			}
		});

		yeuThichImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (like % 2 == 0) {
					yeuThichImageButton
							.setBackgroundResource(R.drawable.yellowstar);
					monNgonDB.Like(title);
					like = 1;

					Toast.makeText(getApplicationContext(), R.string.notiLike,
							Toast.LENGTH_SHORT).show();

				}

				else {
					yeuThichImageButton
							.setBackgroundResource(R.drawable.whitestar);
					monNgonDB.UnLike(title);

					Toast.makeText(getApplicationContext(),
							R.string.notiUnLike, Toast.LENGTH_SHORT).show();
					;

					like = 0;
				}

			}
		});

		nguyenLieuImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (b % 2 == 0) {
					nguyenLieuImageButton
							.setBackgroundResource(R.drawable.btn_nguyenlieu_hien);
					nguyenLieuTextView.setVisibility(View.VISIBLE);
					b = b + 1;
				}

				else {
					nguyenLieuImageButton
							.setBackgroundResource(R.drawable.btn_nguyenlieu_an);
					nguyenLieuTextView.setVisibility(View.GONE);
					b = b + 1;
				}
			}
		});

		cheBienImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (c % 2 == 0) {
					cheBienImageButton
							.setBackgroundResource(R.drawable.btn_cachthuc_hien);
					cheBienTextView.setVisibility(View.VISIBLE);
					c = c + 1;
				}

				else {
					cheBienImageButton
							.setBackgroundResource(R.drawable.btn_cachthuc_an);
					cheBienTextView.setVisibility(View.GONE);
					c = c + 1;
				}
			}
		});

		machNhoImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (d % 2 == 0) {
					machNhoImageButton
							.setBackgroundResource(R.drawable.btn_meovat_hien);
					meoVatTextView.setVisibility(View.VISIBLE);
					d = d + 1;
				}

				else {
					machNhoImageButton
							.setBackgroundResource(R.drawable.btn_meovat_an);
					meoVatTextView.setVisibility(View.GONE);
					d = d + 1;
				}
			}
		});
		
		googleImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				shareGooglePlus();
				
			}
		});
	}
	
	
	private void shareGooglePlus(){
		
		if(isGooglePlusInstalled()){
			Intent share = new Intent(Intent.ACTION_SEND);
			share.setType("text/plain");
			share.setPackage("com.google.android.apps.plus");
			share.putExtra(Intent.EXTRA_TEXT,"\n\n"+"1.Nguyên liệu"+"\n"+nguyenLieuTextView.getText().toString()
					                             +"\n"+"2.Chế biến"+"\n"+cheBienTextView.getText().toString()
					                             +"\n"+"3.Mách nhỏ"+"\n"+meoVatTextView.getText().toString());
			startActivity(share);
		}
		
		else{
			AlertDialog.Builder buider = new AlertDialog.Builder(this);
			buider.setTitle("Thông báo");
			buider.setMessage("Bạn chưa cài đặt GooglePlus. Bạn có muốn cài đặt không?");
			buider.setCancelable(false);
			buider.setPositiveButton("YES", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.plus"));
					startActivity(intent);
					finish();
				}
			});
			buider.setNegativeButton("NO", null);
			
			buider.show();
			
			//Toast.makeText(getApplicationContext(), "ban chua cai dat googleplus", Toast.LENGTH_LONG).show();
		}
		
	}

	private boolean isGooglePlusInstalled() {
		
		try {
			getPackageManager().getApplicationInfo("com.google.android.apps.plus",0);
			return true;
		} catch (PackageManager.NameNotFoundException e) {
			return false;
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		fb.authorizeCallback(requestCode, resultCode, data);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		startAppAd.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		startAppAd.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}

	@SuppressWarnings("deprecation")
	public void shareOnFacebook() {
		Bundle param = new Bundle();
		param.putString("caption", R.string.app_name+"");
		param.putString("link", "http://www.youtube.com/embed/"+youtubeId);
		param.putString("message", "\n\n"+"1.Nguyên liệu"+"\n"+nguyenLieuTextView.getText().toString()
					                             +"\n"+"2.Chế biến"+"\n"+cheBienTextView.getText().toString()
					                             +"\n"+"3.Mách nhỏ"+"\n"+meoVatTextView.getText().toString());
		

		fb.dialog(MonAnActivity.this, "feed", param,
				new DialogListener() {

					@Override
					public void onFacebookError(FacebookError e) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onError(DialogError e) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onComplete(Bundle values) {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(),
								R.string.shareSuccess,
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onCancel() {
						// TODO Auto-generated method stub

					}
				});
	}

}
